package com.iss.card.view.account;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iss.card.CardController;
import com.iss.card.R;
import com.iss.card.common.ApplicationManager;
import com.iss.card.common.FunctionUtil;
import com.iss.card.common.IBackKeyListener;
import com.iss.card.common.Tools;
import com.iss.card.model.UserInfo;
import com.iss.card.view.model.ResponseBean;
import com.iss.card.webservice.BaseModel;
import com.iss.card.webservice.WebServiceAsyncService;
import com.iss.card.webservice.WebServiceAsyncService.Operation;
import com.iss.card.webservice.WebServiceAsyncService.PostExecute;
import com.iss.card.webservice.WebServiceAsyncServiceHelper;
import com.iss.card.webservice.WebServiceManager.WebServiceRegistrationParameter;
import com.iss.card.webservice.WebServiceManager.WebServiceUrlPortManager;
import com.iss.card.webservice.WebServiceUtils;


public class AccountChangePasswordView extends Fragment implements

IBackKeyListener, OnClickListener {

	private Activity mContext;
	private LinearLayout mainView;
	private TextView mOldPassword;
	private TextView mNewPassword;
	private TextView mConfirmPassword;
	private ImageButton btnConfirm;
	private Button btnReturn;
	private Button btnJob;
	private TextView name;
	private JSONObject accountObject;

	private void initView() {
		mOldPassword = (TextView) mainView.findViewById(R.id.old_password);
		mNewPassword = (TextView) mainView.findViewById(R.id.new_password);
		mConfirmPassword = (TextView) mainView
				.findViewById(R.id.confirm_new_password);
		btnReturn = (Button) mainView.findViewById(R.id.return_button);
		name = (TextView) mainView.findViewById(R.id.job_name_val);
		name.setVisibility(View.VISIBLE);
		name.setText(R.string.change_password);
		btnReturn.setVisibility(View.VISIBLE);
		btnReturn.setOnClickListener(this);

		btnConfirm = (ImageButton) mainView.findViewById(R.id.job_button);
		btnConfirm.setOnClickListener(this);
		btnConfirm.setBackgroundResource(R.drawable.feedback_send_selector);
		btnConfirm.setVisibility(View.VISIBLE);
	}

	private void doChangePassword() {
		String[] obj = doValidationAndCollect();
		doAccountRegister(obj);
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mContext = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		try {
			accountObject = new JSONObject(
					FunctionUtil.getUsrInfoForAutoLogin());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		mainView = (LinearLayout) inflater.inflate(R.layout.change_password,
				null);
		initView();
		return mainView;
	}

	/*
	 * ��֤�������Ϣ���ռ����
	 */
	private String[] doValidationAndCollect() {
		String userMoblile = FunctionUtil.getCurrentLoginUserMobile();
		String oldPassword = mOldPassword.getText().toString();
		String newPassword = mNewPassword.getText().toString();
		String confirmPassword = mConfirmPassword.getText().toString();
		if (TextUtils.isEmpty(oldPassword)) {
			Tools.showToast(mContext, R.string.old_password_false);
			return null;
		}

		if (TextUtils.isEmpty(newPassword)) {
			Tools.showToast(mContext, R.string.new_password_false);
			return null;
		}

		if (TextUtils.isEmpty(confirmPassword)
				|| !confirmPassword.equals(newPassword)) {
			Tools.showToast(mContext, R.string.new_password_double_false);
			mNewPassword.setText("");
			mConfirmPassword.setText("");
			return null;
		}

		String[] obj = { userMoblile, oldPassword, newPassword };
		return obj;
	}

	/*
	 * ע��
	 */
	private void doAccountRegister(String[] obj) {
		if (obj == null) {
			return;
		}

		WebServiceAsyncService asyncService = new WebServiceAsyncService();
		asyncService.setParameterToAsyncService(mContext,
				asyncService.getNextToken(),
				Operation.OP_SERVICE_ARG_CHANGE_PASSWORD,
				WebServiceRegistrationParameter.PRODUCT_CHANGE_PASSWORD, obj,
				WebServiceUrlPortManager.ACTION_URL_PORT_CHANGE_PASSWORD, this,
				WebServiceAsyncServiceHelper.TYPE_HTTP_GET_INSTREAM);
		asyncService.setPostExecute(new PostExecute() {
			@Override
			public void onAsyncServiceComplete(int token, Object result,
					int index) {
				/*
				 * result: OK or failed reason
				 */
				ResponseBean response = WebServiceUtils
						.getObjectResultToBaseInfo(result, BaseModel.class);
				if (response.getStatus().equals(
						ApplicationManager.RESPONSE_OK)) {
					hideSoftwareKeyboard();
					Tools.showToast(mContext, R.string.change_password_true);
					String accountMobile = FunctionUtil.getCurrentLoginUserMobile();
					accountObject.remove(accountMobile);
					try {
						accountObject.put(accountMobile,
								mNewPassword.getText().toString());
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					FunctionUtil.saveUsrInfoForAutoLogin(accountObject.toString());
					
					mContext.finish();
					
				} else {
					Tools.showToast(mContext, response.getMessage());
					mOldPassword.setText("");
					mNewPassword.setText("");
					mConfirmPassword.setText("");
					return;
				}
			}
		});
	}

	@Override
	public void cancelUpdate(Object... params) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onDestroy()
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onDestroyView()
	 */
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.return_button:
			mContext.finish();
			break;
		case R.id.job_button:
			doChangePassword();
			break;
		default:
			break;
		}
	}

	private void hideSoftwareKeyboard() {
		((InputMethodManager) mContext
				.getSystemService(Context.INPUT_METHOD_SERVICE))
				.hideSoftInputFromWindow(mContext.getCurrentFocus().getWindowToken(), 0);
	}
}
